{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find Closest Number to Zero"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findClosestNumber"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #找到最接近 0 的数字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个长度为 <code>n</code>&nbsp;的整数数组&nbsp;<code>nums</code>&nbsp;，请你返回 <code>nums</code>&nbsp;中最 <strong>接近</strong>&nbsp;<code>0</code>&nbsp;的数字。如果有多个答案，请你返回它们中的 <strong>最大值</strong>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [-4,-2,1,4,8]\n",
    "<b>输出：</b>1\n",
    "<strong>解释：</strong>\n",
    "-4 到 0 的距离为 |-4| = 4 。\n",
    "-2 到 0 的距离为 |-2| = 2 。\n",
    "1 到 0 的距离为 |1| = 1 。\n",
    "4 到 0 的距离为 |4| = 4 。\n",
    "8 到 0 的距离为 |8| = 8 。\n",
    "所以，数组中距离 0 最近的数字为 1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [2,-1,1]\n",
    "<b>输出：</b>1\n",
    "<b>解释：</b>1 和 -1 都是距离 0 最近的数字，所以返回较大值 1 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 1000</code></li>\n",
    "\t<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-closest-number-to-zero](https://leetcode.cn/problems/find-closest-number-to-zero/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-closest-number-to-zero](https://leetcode.cn/problems/find-closest-number-to-zero/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[-4,-2,1,4,8]', '[2,-1,1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        closest = float('inf')  # 初始化最小距离为正无穷大\n",
    "        result = None  # 初始化结果为None\n",
    "        for num in nums:\n",
    "            distance = abs(num)\n",
    "            if distance < closest:\n",
    "                closest = distance\n",
    "                result = num\n",
    "            elif distance == closest:\n",
    "                result = max(result, num)\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        for i in range(1,len(nums)):\n",
    "            if abs(nums[i])>abs(nums[i-1]):\n",
    "                return nums[i-1]\n",
    "        return nums[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        now=99999999\n",
    "        for i in range(len(nums)):\n",
    "            if abs(nums[i])<now:\n",
    "                now=abs(nums[i])        \n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==abs(now):\n",
    "                return nums[i]\n",
    "        return -now\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        ans = []\n",
    "        m = inf\n",
    "\n",
    "        for t in nums:\n",
    "            n = abs(t)\n",
    "            if n < m:\n",
    "                m = n\n",
    "                ans = []\n",
    "                ans.append(t)\n",
    "            elif n == m:\n",
    "                ans.append(t)\n",
    "            else:\n",
    "                continue\n",
    "\n",
    "        return max(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        res = inf\n",
    "        for num in nums:\n",
    "            if abs(num) < abs(res):\n",
    "                res = num\n",
    "            elif abs(num) == abs(res):\n",
    "                res = max(num, res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        res = nums[0]\n",
    "        for i in nums[1:]:\n",
    "            if abs(i) < abs(res):\n",
    "                res = i\n",
    "            elif i > 0 and i == -res:\n",
    "                res = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        t = min(map(abs, nums))\n",
    "        if t not in nums:\n",
    "            t = -t\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        if not nums:\n",
    "            return None\n",
    "        res = nums[0]\n",
    "        for k in nums:\n",
    "            if k == 0:\n",
    "                return 0\n",
    "            if abs(k) < abs(res):\n",
    "                res = k\n",
    "            if abs(k) == abs(res):\n",
    "                res = max(k, res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        res = nums[0]   # 已遍历元素中绝对值最小且数值最大的元素\n",
    "        dis = abs(nums[0])   # 已遍历元素的最小绝对值\n",
    "        for num in nums:\n",
    "            if abs(num) < dis:\n",
    "                dis = abs(num)\n",
    "                res = num\n",
    "            elif abs(num) == dis:\n",
    "                res = max(res, num)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        temp = 1000001\n",
    "        for num in nums:\n",
    "            if abs(num) < abs(temp):\n",
    "                temp = num\n",
    "            elif abs(num) == abs(temp):\n",
    "                if temp < num:\n",
    "                    temp = num\n",
    "        return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        low, high = 0, len(nums) - 1\n",
    "        while low < high:\n",
    "            m = low + (high - low) // 2\n",
    "            if nums[m] == 0:\n",
    "                return nums[m]\n",
    "            elif nums[m] < 0:\n",
    "                low = m + 1\n",
    "            elif nums[m] > 0:\n",
    "                high = m\n",
    "        a = low\n",
    "        if a == 0:\n",
    "            return nums[a]\n",
    "        else:\n",
    "            return nums[a] if abs(nums[a]) <= abs(nums[a - 1]) else nums[a - 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        dic={}\n",
    "        # \n",
    "        a=float('-inf')\n",
    "        b=float('inf')\n",
    "        for i in nums:\n",
    "            if i>0:\n",
    "                b=min(b,i)\n",
    "            elif i<0:\n",
    "                a=max(a,i)\n",
    "            else:\n",
    "                return 0\n",
    "        if b<=abs(a):\n",
    "            return b\n",
    "        else:\n",
    "            return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        for i in range(0, max(abs(max(nums)), abs(min(nums))) + 1):\n",
    "            if i in nums:\n",
    "                return i\n",
    "            elif -i in nums:\n",
    "                return -i\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        res = nums[0]\n",
    "        dis = abs(nums[0])\n",
    "        for num in nums:\n",
    "            if abs(num) < dis:\n",
    "                dis = abs(num)\n",
    "                res = num\n",
    "            elif abs(num) == dis:\n",
    "                res = max(res, num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        r=math.inf\n",
    "        for i in nums:\n",
    "            r=min(r,abs(i))\n",
    "        if r in nums:\n",
    "            return r\n",
    "        else:\n",
    "            return -r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        i = bisect_left(nums, 0)\n",
    "        if i == len(nums):\n",
    "            return nums[-1]\n",
    "        return nums[i-1] if abs(nums[i-1]) < nums[i] else nums[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        minn=abs(nums[0])\n",
    "        for i in range(len(nums)):\n",
    "            minn=min(minn,abs(nums[i]))\n",
    "        if minn in nums:\n",
    "            return minn \n",
    "        else:\n",
    "            return -minn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        x = min(abs(n) for n in nums)\n",
    "        return x if x in nums else -x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        dist = inf\n",
    "        res = 0\n",
    "        for num in nums:\n",
    "            if abs(num) < dist or (abs(num) == dist and num > res):\n",
    "                res = num\n",
    "                dist = abs(num)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        res = nums[0]\n",
    "        dis = abs(nums[0])\n",
    "        for num in nums:\n",
    "            if abs(num) < dis:\n",
    "                dis = abs(num)\n",
    "                res = num\n",
    "            elif abs(num) == dis:\n",
    "                res = max(res, num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        x = min(abs(n) for n in nums)\n",
    "        return x if x in nums else -x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        dist = inf\n",
    "        res = 0\n",
    "        for num in nums:\n",
    "            if abs(num) < dist:\n",
    "                res = num\n",
    "                dist = abs(num)\n",
    "            elif abs(num) == dist and num > res:\n",
    "                res = num\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, a: List[int]) -> int:\n",
    "        res = -inf\n",
    "        for x in a:\n",
    "            if abs(x) < abs(res) or (abs(x) == abs(res) and x > res):\n",
    "                res = x\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        res = float('inf')\n",
    "        a = 0\n",
    "        for i in nums:\n",
    "            if abs(i-0) < res:\n",
    "                res = abs(i-0)\n",
    "                a = i\n",
    "            if abs(i-0) == res:\n",
    "                if a > i:\n",
    "                    a = a\n",
    "                else:\n",
    "                    a = i\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        i = bisect_left(nums, 0)\n",
    "        if i == len(nums):\n",
    "            i = -1\n",
    "        if abs(nums[i-1]) < nums[i]:\n",
    "            return nums[i-1]\n",
    "        return nums[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums_abs = []\n",
    "        length = len(nums)\n",
    "        for i in range(length):\n",
    "            if nums[i] < 0:\n",
    "                nums_abs.append(-nums[i])\n",
    "            else:\n",
    "                nums_abs.append(nums[i])\n",
    "\n",
    "        min_num = min(nums_abs)\n",
    "        if min_num in nums:\n",
    "            return min_num\n",
    "        else:\n",
    "            return -min_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        res = nums[0]\n",
    "        dis = abs(nums[0])\n",
    "        for num in nums:\n",
    "            if abs(num) < dis:\n",
    "                dis = abs(num)\n",
    "                res = num\n",
    "            elif abs(num) == dis:\n",
    "                res = max(res, num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        # idx = 0\n",
    "        ans = inf\n",
    "        for nu in nums:\n",
    "            if abs(nu) < abs(ans) or (abs(nu) == abs(ans) and nu >= 0):\n",
    "                ans = nu\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        dis=math.inf\n",
    "        res=-math.inf\n",
    "        for num in nums:\n",
    "            if dis>abs(num):\n",
    "                dis=abs(num)\n",
    "                res=num\n",
    "            elif dis==abs(num):\n",
    "                res=max(num,res)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        min_dif = float('inf')\n",
    "        max_num = float('-inf')\n",
    "        for x in nums:\n",
    "            cur_dif = abs(x)\n",
    "            if cur_dif < min_dif:  # 如果遍历到绝对值更小的数，更新最小距离和当前数字\n",
    "                min_dif = cur_dif\n",
    "                max_num = x\n",
    "            elif cur_dif == min_dif:  # 如果遍历到一个数，它的距离和最小距离一样，则更新最大数\n",
    "                max_num = max(max_num, x)\n",
    "\n",
    "        return max_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        res = nums[0]   # 已遍历元素中绝对值最小且数值最大的元素\n",
    "        dis = abs(nums[0])   # 已遍历元素的最小绝对值\n",
    "        for num in nums:\n",
    "            if abs(num) < dis:\n",
    "                dis = abs(num)\n",
    "                res = num\n",
    "            elif abs(num) == dis:\n",
    "                res = max(res, num)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        return min(nums, key=lambda n:abs(n-0.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        return min(nums, key=lambda n:abs(n-0.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        low, high = 0, len(nums) - 1\n",
    "        while low < high:\n",
    "            m = low + (high - low) // 2\n",
    "            if nums[m] == 0:\n",
    "                return nums[m]\n",
    "            elif nums[m] < 0:\n",
    "                low = m + 1\n",
    "            elif nums[m] > 0:\n",
    "                high = m\n",
    "        if low == 0:\n",
    "            return nums[low]\n",
    "        else:\n",
    "            return nums[low] if abs(nums[low]) <= abs(nums[low - 1]) else nums[low - 1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        min_d, k = inf, 2\n",
    "        for i in nums:\n",
    "            if abs(i) < min_d:\n",
    "                min_d = abs(i)\n",
    "                k = i\n",
    "            elif abs(i) == min_d:\n",
    "                k = max(i,k)\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        num = nums[0]\n",
    "        for i in nums:\n",
    "            if abs(num) > abs(i):\n",
    "                num = i\n",
    "            elif abs(num) == abs(i) and i > num:\n",
    "                num = i\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        ns = sorted(nums,key=lambda x:abs(x))\n",
    "        dis = abs(ns[0])\n",
    "        ans = ns[0]\n",
    "        for i in ns:\n",
    "            if abs(i) > dis:\n",
    "                return ans\n",
    "            elif abs(i) == dis and i > ans:\n",
    "                ans = i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        x = min(abs(n) for n in nums)\n",
    "        return x if x in nums else -x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        value = 100001\n",
    "        distance = 100001\n",
    "        for num in nums:\n",
    "            temp = abs(num)\n",
    "            if temp > distance:\n",
    "                continue\n",
    "            if temp == distance:\n",
    "                if value < num:\n",
    "                    value = num\n",
    "                continue\n",
    "            value = num\n",
    "            distance = temp\n",
    "        return value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        res=nums[0]\n",
    "        abs_=abs(nums[0])\n",
    "        for num in nums:\n",
    "            if abs(num)<abs_:\n",
    "                abs_=abs(num)\n",
    "                res=num\n",
    "            elif abs(num)==abs_:\n",
    "                res=max(res,num)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        left, right = -abs(nums[0]), abs(nums[0])\n",
    "        for num in nums:\n",
    "            if num > right or num < left:\n",
    "                continue\n",
    "            if right > num >= 0:\n",
    "                right = num\n",
    "            if 0 >= num > left:\n",
    "                left = num\n",
    "        if right != abs(nums[0]) or left != -abs(nums[0]):\n",
    "            return left if abs(left) < right else right\n",
    "        else:\n",
    "            return nums[0] if right not in nums else right\n",
    "\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        l1=nums.copy()\n",
    "        for i in range(len(nums)):\n",
    "            nums[i]=abs(nums[i])\n",
    "        min1=min(nums)\n",
    "        l2=[]\n",
    "        for i in l1:\n",
    "            if abs(i)==min1:\n",
    "                l2.append(i)\n",
    "        return max(l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "\n",
    "        return min(nums, key=lambda n:abs(n-0.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        a = min(list(map(func_abs,nums)))\n",
    "        return a if a in nums else -a\n",
    "\n",
    "def func_abs(x):\n",
    "    return abs(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        dis = 10^5\n",
    "        res = []\n",
    "        for i in nums:\n",
    "            if abs(min(nums,key=abs)) == abs(i):\n",
    "                res.append(i)\n",
    "        res.sort()\n",
    "        return res[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        ans=nums[0]\n",
    "        for num in nums:\n",
    "            if abs(ans)>abs(num) or abs(ans)==abs(num) and ans<num:\n",
    "                ans=num\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        return min(nums, key=lambda n:abs(n-0.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        lst = [(i, abs(x)) for i, x in enumerate(nums)]\n",
    "        lst.sort(key=lambda x: x[1])\n",
    "        min_abs = lst[0][1]\n",
    "        res = [nums[x[0]] for x in lst if x[1] == min_abs]\n",
    "        return max(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        res = -inf\n",
    "        for i in nums:\n",
    "            if abs(i) <= abs(res):\n",
    "                res = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        return sorted(nums,key = lambda x:(abs(x),-x))[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        lt_0_max = float('-inf')\n",
    "        gt_0_min = float('inf')\n",
    "    \n",
    "        for i in nums:\n",
    "            if i == 0:\n",
    "                return 0\n",
    "            elif i > 0:\n",
    "                gt_0_min = min(gt_0_min,i)\n",
    "            else:\n",
    "                lt_0_max = max(lt_0_max,i)\n",
    "        \n",
    "        if abs(lt_0_max) == gt_0_min:\n",
    "            return gt_0_min\n",
    "        elif abs(lt_0_max) < gt_0_min:\n",
    "            return lt_0_max\n",
    "        return gt_0_min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        dist, num = float(\"inf\"), -1\n",
    "        for n in nums:\n",
    "            d = abs(n)\n",
    "            if d < dist:\n",
    "                num = n\n",
    "                dist = d\n",
    "            elif d == dist:\n",
    "                num = max(num,n)\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        \n",
    "        return -sorted([[abs(i), -i] for i in nums])[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res,ans = float(\"inf\"),0\n",
    "        for num in nums:\n",
    "            if abs(num-0)<=res:\n",
    "                res = abs(num-0)\n",
    "                ans = num\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort(key=lambda x: [abs(x), -x])\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def findClosestNumber(self, nums: List[int]) -> int:\n",
    "#         ans,dis=float(\"-inf\"),float(\"inf\")\n",
    "#         for num in nums:\n",
    "#             if abs(num)<dis:\n",
    "#                 dis=abs(num)\n",
    "#                 ans=num\n",
    "#             elif abs(num)==dis:\n",
    "#                 ans=max(ans,num)\n",
    "#         return ans\n",
    "\n",
    "### 官解：遍历\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        res = nums[0]   # 已遍历元素中绝对值最小且数值最大的元素\n",
    "        dis = abs(nums[0])   # 已遍历元素的最小绝对值\n",
    "        for num in nums:\n",
    "            if abs(num) < dis:\n",
    "                dis = abs(num)\n",
    "                res = num\n",
    "            elif abs(num) == dis:\n",
    "                res = max(res, num)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        res = nums[0]   # 已遍历元素中绝对值最小且数值最大的元素\n",
    "        dis = abs(nums[0])   # 已遍历元素的最小绝对值\n",
    "        for num in nums:\n",
    "            if abs(num) < dis:\n",
    "                dis = abs(num)\n",
    "                res = num\n",
    "            elif abs(num) == dis:\n",
    "                res = max(res, num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        a = sorted(nums,key = lambda x:abs(x))\n",
    "        return abs(a[0]) if abs(a[0]) in nums else -abs(a[0]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        return sorted(nums, key = lambda x: (abs(x), -x))[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        m = {}\n",
    "        for num in nums:\n",
    "            if abs(num) not in m.keys():\n",
    "                m[abs(num)] = [num]\n",
    "            else:\n",
    "                m[abs(num)].append(num)\n",
    "        sm = sorted(m.items(), key=lambda row: row[0])\n",
    "        ls = sm[0][1]\n",
    "        ls.sort(reverse=True)\n",
    "        return ls[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        sorted_list = sorted(nums, key=lambda x: (abs(x), -x))\n",
    "        \n",
    "        # 选择绝对值最接近0的数\n",
    "        return sorted_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        return sorted(nums, key = lambda x: (abs(x), -x))[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort(key = lambda x : [abs(x), -x])\n",
    "        return nums[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort(key=lambda x: [abs(x), -x])\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        lst=[]\n",
    "        dic={}\n",
    "        \n",
    "        for i in nums:\n",
    "\n",
    "            dic[abs(i)]=i\n",
    "            lst.append(abs(i))\n",
    "        \n",
    "        small=min(lst)\n",
    "        ans=dic.get(small)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        ns = sorted(nums,key=lambda x:abs(x))\n",
    "        ans = ns[0]\n",
    "        if abs(ans) in set(ns):\n",
    "            return abs(ans)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findClosestNumber(self, nums: List[int]) -> int:\n",
    "        list_=[abs(x) for x in nums]\n",
    "        list_.sort()\n",
    "        a=list_[0]\n",
    "        if a in nums:\n",
    "            return a\n",
    "        return -a"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
